const app = getApp()
const utils = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    skin: '',
    oxtId : 0,
    typeId : 0,
    haveNext: false,
    page: 1,
    dataList: []
  },

  toPageList(event) {
    console.log(event.currentTarget.dataset);
    var that = this;
    var areaId = event.currentTarget.dataset.areaId || 0;
    var jumpUrl = '/pages/equipment/list?oxtId=' + that.data.oxtId + "&typeId=" + that.data.typeId + "&areaId=" + areaId;
    console.log(jumpUrl);
    if (that.data.typeId == 4) {
      jumpUrl = '/pages/spray/devicelist?oxtId=' + that.data.oxtId + "&typeId=" + that.data.typeId + "&areaId=" + areaId;
    }
    wx.navigateTo({
      url: jumpUrl
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      typeId: options.typeId || 0,
      oxtId : options.oxtId || 0,
      page : 1
    });
    this.loadList(1);

    app.setNavBar()
    const { skin } = app.globalData
    this.setData({
      skin
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.haveNext) {
      this.setData({
        page: this.data.page + 1
      });
      this.loadList(this.data.page);
    }
  },
  loadList: function (page) {
    let that = this;
    let getOxtallAreaListParams = {
      "typeId": that.data.typeId,
      "oxtallId" : that.data.oxtId,
      "page": page,
      "pageSize": 8,
    };
    utils.promisePost('/wx/property/getOxtallAreaList', getOxtallAreaListParams).then(res => {
      console.log(res);
      let oxtallAreaList = that.data.dataList;
      let haveNext = that.data.haveNext;
      if (page == 1) {
        oxtallAreaList = [];
      }
      if (res.code == 200) {
        let result = res.data;
        if (result.rows.length > 0) {
          for (let i = 0; i < result.rows.length; i++) {
            let itemRow = {
              "name" : result.rows[i].oxtaName,
              "normalSum" : result.rows[i].online,
              "errorSum" : result.rows[i].offline,
              "oxtaId" : result.rows[i].oxtaId
            };
            if (that.data.typeId == 4) { //精准喷淋
              itemRow.normalSum = result.rows[i].normal;
              itemRow.errorSum = result.rows[i].abnormal;
            }
            oxtallAreaList.push(itemRow);
          }
          haveNext = true;
        } else {
          haveNext = false;
        }
        console.log(oxtallAreaList);
      } else {
      }
      this.setData({
        dataList: oxtallAreaList,
        haveNext: haveNext
      });
    }).catch(e => {
      console.log(e);
    });
  }
})